﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.ParentForm;
using Com.Seezt.Skins;
using UI.Lib;
using Components;
using System.Collections;
using UI.Lib.Factory;

namespace UI.SellOrder
{
    public partial class FrmSellOrderQuery : BaseQuery
    {
        public FrmSellOrderQuery(string modulecode, string action) :
            base(modulecode, action)
        {
            InitializeComponent();
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("sellercode" == name) //销售商
            {
                where = string.Format(" status ='1' {0} ", Context.GetAppKeyWhere("usercode", true));
            }
            else if ("shopcode" == name) //门店
            {
                string sellerid = Validation.ToString(this.tbSummary.GetValue("sellid"));
                if (string.IsNullOrEmpty(sellerid))
                {
                    MsgBox.Show(this, "请先选择销售商！");
                    this.tbSummary.Focus("sellercode");
                    return false;
                }

                where = string.Format("slistid = '{0}' {1} ", sellerid, Context.GetAppKeyWhere("muser", true));
            }
            else if ("areacode" == name) //区域
            {
                where = Context.GetAppKeyWhere("muser");
            }
            else if ("bigclass" == name) //大类
            {
                where = string.Format(" status ='1' {0} ", Context.GetAppKeyWhere("muser", true));
            }
            else if ("smallclass" == name) //小类
            {
                string bigclassid = Validation.ToString(this.tbSummary.GetValue("bigclassid"));
                if (string.IsNullOrEmpty(bigclassid))
                {
                    MsgBox.Show(this, "请先选择大类！");
                    this.tbSummary.Focus("bigclass");
                    return false;
                }

                where = string.Format("plevercode = '{0}' AND status = '1' {1} ", bigclassid, Context.GetAppKeyWhere("muser", true));
            }

            return true;
        }

        public override void AfterPopWin(string name, System.Collections.Hashtable data, bool ischange)
        {
            if (ischange)
            {
                if ("bigclass" == name)
                {
                    this.tbSummary.SetValue("smallclassid", "");
                    this.tbSummary.SetValue("smallclass", "");
                    this.tbSummary.SetValue("smallclassdesc", "");
                }
            }
        }


        protected override void DoSearch()
        {
            this.dgvDtl.Columns["bigclass"].Visible = false;
            this.dgvDtl.Columns["bigclassname"].Visible = false;
            this.dgvDtl.Columns["sclass"].Visible = false;
            this.dgvDtl.Columns["sclassname"].Visible = false;
            this.dgvDtl.Columns["sellcode"].Visible = false;
            this.dgvDtl.Columns["sellname"].Visible = false;
            this.dgvDtl.Columns["shopcode"].Visible = false;
            this.dgvDtl.Columns["shopname"].Visible = false;
            this.dgvDtl.Columns["area"].Visible = false;
            this.dgvDtl.Columns["areaname"].Visible = false;
            this.dgvDtl.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            string sellid = Validation.ConvertNullToEmpty(this.tbSummary.GetValue("sellid"));
            if (string.IsNullOrEmpty(sellid) == false)
            {
                this.dgvDtl.Columns["sellcode"].Visible = true;
                this.dgvDtl.Columns["sellname"].Visible = true;

                this.dgvDtl.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            }

            string shopid = Validation.ConvertNullToEmpty(this.tbSummary.GetValue("shopid"));
            if (string.IsNullOrEmpty(shopid) == false)
            {
                this.dgvDtl.Columns["shopcode"].Visible = true;
                this.dgvDtl.Columns["shopname"].Visible = true;

                this.dgvDtl.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            }

            string big = Validation.ConvertNullToEmpty(this.tbSummary.GetValue("bigclassid"));
            if (string.IsNullOrEmpty(big) == false)
            {
                this.dgvDtl.Columns["bigclass"].Visible = true;
                this.dgvDtl.Columns["bigclassname"].Visible = true;
                this.dgvDtl.Columns["sclass"].Visible = true;
                this.dgvDtl.Columns["sclassname"].Visible = true;

                this.dgvDtl.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            }

            string areaid = Validation.ConvertNullToEmpty(this.tbSummary.GetValue("areaid"));
            if (string.IsNullOrEmpty(areaid) == false)
            {
                this.dgvDtl.Columns["area"].Visible = true;
                this.dgvDtl.Columns["areaname"].Visible = true;
              
                this.dgvDtl.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            }


            string paihang = Validation.ToString(this.tbSummary.GetValue("phfield"));

            Hashtable where = new Hashtable();
            where["i"] = Validation.ToInt(this.tbSummary.GetValue("selltop")); //前几条
            where["pid"] = ""; //控件ID
            where["startdate"] = Validation.ToDate(this.tbSummary.GetValue("startdate"), Global.MinDate); //控件ID
            where["endate"] = Validation.ToDate(this.tbSummary.GetValue("endate"), Global.MaxDate); //结束日期
            where["sellid"] = sellid;
            where["shopid"] = shopid;
            where["areaid"] = areaid;
            where["smallid"] = Validation.ConvertNullToEmpty(this.tbSummary.GetValue("smallclassid"));
            where["bigid"] = big;
            where["brandid"] = Validation.ConvertNullToEmpty(this.tbSummary.GetValue("brandid"));
            where["paihang"] = ("Q" == paihang) ? "sellqty" : (("A" == paihang) ? "sellamt" : "sellpct");

            DataTable dt = CommonHelper.Entity.ExecFun("getsellorderquerydata", where);

            BinderGrid(dt);
        }
    }
}
